package com.leon.android.arch.mvvv.base

import android.content.pm.ActivityInfo
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.leon.android.common.utils.StatusBarUtil
import kotlinx.coroutines.*
abstract class BaseActivity : AppCompatActivity() {

    protected abstract val layoutId: Int

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(layoutId)
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        setStatusBar()
        initView(savedInstanceState)
        initData(savedInstanceState)
        bindModel()
    }

    protected open fun setStatusBar() {
        StatusBarUtil.setTransparent(this)
        StatusBarUtil.setLightMode(this)
    }

    protected abstract fun initData(savedInstanceState: Bundle?)

    protected abstract fun initView(savedInstanceState: Bundle?)


    protected open fun getViewModelFactory(): ViewModelProvider.Factory? {
        return null
    }

    fun launch(
        block: suspend CoroutineScope.() -> Unit
    ): Job {
        return lifecycleScope.launch {
            block()
        }
    }

    fun io(
        block: suspend CoroutineScope.() -> Unit
    ): Job {
        return lifecycleScope.launch {
            block()
        }
    }


    protected abstract fun bindModel()


}